﻿/******************************************************************************
 * Name         : LoginPlayerManager.cpp
 * Author       : 671643387
 * Date         : 2015年9月14日
 * Description  :
 *****************************************************************************/

#include <cassert>
#include "../Config.hpp"
#include "LoginPlayerManager.hpp"
using namespace sxr::login_server;

static LoginPlayerManager sLoginPlayerManager;
LoginPlayerManager* LoginPlayerManager::getMe(void)
{
	return &sLoginPlayerManager;
}

LoginPlayerManager::LoginPlayerManager()
: playerFactory_(new LoginPlayerFactory())
, players_(LOGIN_PLAYER_MAP_INIT_SIZE)
{

}

LoginPlayerManager::~LoginPlayerManager()
{

}

bool LoginPlayerManager::initialize()
{
	return true;
}

void LoginPlayerManager::cleanup()
{
    //// 释放资源
    playerFactory_.reset();
}

bool LoginPlayerManager::newPlayer(dev::net::TcpConnectionPtr& conn)
{
    LoginPlayerPtr player = playerFactory_->create(conn->getId(), conn);
    if (player) {
        players_[player->getId()] = player;
        return true;
    }
	return false;
}

void LoginPlayerManager::removePlayer(LoginPlayer& player)
{
    LoginPlayerPtr playerPtr = players_[player.getId()];
    if (playerPtr)
    {
        players_.erase(player.getId());
        return;
    }

    // TODO: CHECK
    assert(false);
}
